package com.wyl.main

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.wyl.libbase.base.BindingActivity
import com.wyl.main.alarm.AlarmFragment
import com.wyl.main.databinding.MainActivityBinding

class MainActivity : BindingActivity<MainActivityBinding>() {

    override fun getLayoutId() = R.layout.main_activity

    override fun loadData() {

    }

    override fun initView() {
        binding.tabLayout.apply {
            addOnTabSelectedListener(tabSelectedListener)
            setupWithViewPager(binding.viewPager)
        }

        binding.viewPager.apply {
            adapter = pagerAdapter
            offscreenPageLimit = fragments.size - 1
        }

    }


    private val tabSelectedListener by lazy {
        object : TabLayout.ViewPagerOnTabSelectedListener(binding.viewPager) {
            override fun onTabReselected(tab: TabLayout.Tab) {

            }
        }
    }


    private val titles by lazy {
        listOf(
            getString(R.string.main_tab1),
            getString(R.string.main_tab2),
            getString(R.string.main_tab3),
            getString(R.string.main_tab4)
        )
    }

    private val fragments by lazy {
        listOf(
            ClockFragment(),
            AlarmFragment(),
            ClockFragment(),
            ClockFragment()
        )
    }

    private val pagerAdapter by lazy { MyPagerAdapter() }

    inner class MyPagerAdapter : FragmentPagerAdapter(supportFragmentManager) {
        override fun getItem(position: Int): Fragment = fragments[position]

        override fun getCount(): Int = fragments.size

        override fun getPageTitle(position: Int): CharSequence? = titles[position]

    }

}
